#encoding:utf-8
'''
Created on 2012/2/2
此為從attribute + class的檔案中取出sample的程式
並且將取出的class輸出到參數三
@author: Netdb_Heng
'''
#參數一為母體，參數二為輸入Label，參數三為輸出sample，參數四為輸出sample的class，參數五為筆數

import sys,random

print ( sys.argv[1] + " to " + sys.argv[2] + ":" + sys.argv[3])

inf = open(sys.argv[1])
infattrs = open(sys.argv[2])
infclass = open(sys.argv[3])

outf_1 = open(sys.argv[4],'w')
outattrs_1 = open(sys.argv[5],'w')
outclass_1 = open(sys.argv[6],'w')

outf_2 = open(sys.argv[7],'w')
outattrs_2 = open(sys.argv[8],'w')
outclass_2 = open(sys.argv[9],'w')

outf_3 = open(sys.argv[10],'w')
outattrs_3 = open(sys.argv[11],'w')
outclass_3 = open(sys.argv[12],'w')

outf_4 = open(sys.argv[13],'w')
outattrs_4 = open(sys.argv[14],'w')
outclass_4 = open(sys.argv[15],'w')

outf_5 = open(sys.argv[16],'w')
outattrs_5 = open(sys.argv[17],'w')
outclass_5 = open(sys.argv[18],'w')

outf_6 = open(sys.argv[19],'w')
outattrs_6 = open(sys.argv[20],'w')
outclass_6 = open(sys.argv[21],'w')

outf_7 = open(sys.argv[22],'w')
outattrs_7 = open(sys.argv[23],'w')
outclass_7 = open(sys.argv[24],'w')

outf_8 = open(sys.argv[25],'w')
outattrs_8 = open(sys.argv[26],'w')
outclass_8 = open(sys.argv[27],'w')

outf_9 = open(sys.argv[28],'w')
outattrs_9 = open(sys.argv[29],'w')
outclass_9 = open(sys.argv[30],'w')

outf_10 = open(sys.argv[31],'w')
outattrs_10 = open(sys.argv[32],'w')
outclass_10 = open(sys.argv[33],'w')

sample_num = int(sys.argv[34])
choose_record = []      #挑選過的

lines = inf.readlines()
class_lines = infclass.readlines()
attrs_lines = infattrs.readlines()
outf_1.write(lines[0])
change=0
while len(choose_record)<sample_num:
    choose=0
    random_int = random.randint(1,len(lines)-1)     #隨機1~len(lines)
    temp = lines[random_int].split()
    tempClass = class_lines[random_int-1].split()
    tempAttrs = attrs_lines[random_int-1].split()
    for j in range(len(choose_record)):
        if random_int == choose_record[j]:
            choose=1
            break
    if choose==0:
        if change<1000:
            choose_record.append(random_int)
            outf_1.write(temp[0] + '\n')
            outclass_1.write(tempClass[0] + '\n')
            outattrs_1.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<2000):
            choose_record.append(random_int)
            outf_2.write(temp[0] + '\n')
            outclass_2.write(tempClass[0] + '\n')
            outattrs_2.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<3000):
            choose_record.append(random_int)
            outf_3.write(temp[0] + '\n')
            outclass_3.write(tempClass[0] + '\n')
            outattrs_3.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<4000):
            choose_record.append(random_int)
            outf_4.write(temp[0] + '\n')
            outclass_4.write(tempClass[0] + '\n')
            outattrs_4.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<5000):
            choose_record.append(random_int)
            outf_5.write(temp[0] + '\n')
            outclass_5.write(tempClass[0] + '\n')
            outattrs_5.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<6000):
            choose_record.append(random_int)
            outf_6.write(temp[0] + '\n')
            outclass_6.write(tempClass[0] + '\n')
            outattrs_6.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<7000):
            choose_record.append(random_int)
            outf_7.write(temp[0] + '\n')
            outclass_7.write(tempClass[0] + '\n')
            outattrs_7.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<8000):
            choose_record.append(random_int)
            outf_8.write(temp[0] + '\n')
            outclass_8.write(tempClass[0] + '\n')
            outattrs_8.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<9000):
            choose_record.append(random_int)
            outf_9.write(temp[0] + '\n')
            outclass_9.write(tempClass[0] + '\n')
            outattrs_9.write(tempAttrs[0] + '\n')
            change+=1
        elif (change<10000):
            choose_record.append(random_int)
            outf_10.write(temp[0] + '\n')
            outclass_10.write(tempClass[0] + '\n')
            outattrs_10.write(tempAttrs[0] + '\n')
            change+=1
inf.close();
outf_1.close();

